package com.tocean.app.service.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tocean.app.controller.request.paper.PaperOperateDto;
import com.tocean.app.controller.request.paper.PaperSearchDto;
import com.tocean.app.controller.request.paper.PaperTopicDto;
import com.tocean.app.dto.response.BasePageResponse;
import com.tocean.app.entity.common.PaperTopic;
import com.tocean.app.entity.common.TestPaper;
import com.tocean.app.mapper.common.PaperMapper;
import com.tocean.app.mapper.common.PaperTopicMapper;
import com.tocean.app.util.id.UUIDGenerate;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PaperService {

    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private PaperTopicMapper paperTopicMapper;


    public BasePageResponse<TestPaper> page(PaperSearchDto params, Integer page, Integer size) {
        Page<TestPaper> _page = new Page(page,size);
        IPage<TestPaper> list = paperMapper.getList(_page,params);
        return new BasePageResponse().setTotal(list.getTotal()).setData(list.getRecords());
    }
    public BasePageResponse<TestPaper> pageByValue(PaperSearchDto params, Integer page, Integer size) {
        Page<TestPaper> _page = new Page(page,size);
        IPage<TestPaper> list = paperMapper.getListByValue(_page,params);
        return new BasePageResponse().setTotal(list.getTotal()).setData(list.getRecords());
    }

    public PaperOperateDto findPaperById(String paperId) {


        TestPaper _paper = paperMapper.selectById(paperId);
        PaperOperateDto paper = new PaperOperateDto(paperId,_paper.getPaperName(),_paper.getPaperSubject(),_paper.getPaperGrade(),_paper.getPaperType());
        return paper;
    }

    public void saveOrUpdate(PaperOperateDto operateDto) {
        TestPaper _testPaper;

        if (StringUtils.isEmpty(operateDto.getId())) {
            _testPaper = new TestPaper();
            _testPaper.setPaperId(UUIDGenerate.getShortUUIDString());
            _testPaper.setPaperName(operateDto.getName());
            _testPaper.setPaperSubject(operateDto.getSubject());
            _testPaper.setPaperGrade(operateDto.getGroup());
            _testPaper.setPaperType(operateDto.getType());

            paperMapper.insert(_testPaper);
        } else {
            TestPaper testPaper = paperMapper.selectById(operateDto.getId());
            _testPaper = testPaper;
            _testPaper.setPaperName(operateDto.getName());
            _testPaper.setPaperSubject(operateDto.getSubject());
            _testPaper.setPaperGrade(operateDto.getGroup());
            _testPaper.setPaperType(operateDto.getType());
            paperMapper.updateById(_testPaper);
        }
    }


    public int isHasStem(String paperId,String stemId) {
        PaperTopic paperTopic =paperTopicMapper.isHasStem(paperId,stemId);
        return paperTopic==null?0:1;
    }

    public void addStem(PaperTopicDto paperTopicDto)
    {
        PaperTopic paperTopic;
        for(int i=0;i<paperTopicDto.getTopics().size();i++) {
            paperTopic = new PaperTopic();
            paperTopic.setPaperId(paperTopicDto.getPaperId()).setTopicId(paperTopicDto.getTopics().get(i));
            paperTopicMapper.insert(paperTopic);
        }
    }

    public PaperTopicDto findPaperTopicById(String paperId) {
        PaperTopicDto paperTopicDto = new PaperTopicDto();
        paperTopicDto.setPaperId(paperId);
        List<String> list =  paperTopicMapper.selectByPaperId(paperId);
        paperTopicDto.setTopics(list);

        return paperTopicDto;
    }


    public void delete(String paperId) {
        try {
            paperMapper.deleteById(paperId);

        }catch (Exception e) {

        }
        try {
            paperTopicMapper.deleteByPaperId(paperId);

        }catch (Exception e) {

        }
    }


    public void deleteTopic(String paperId ) {
        try {
            paperTopicMapper.deleteByPaperId(paperId);
        }catch (Exception e) {

        }
    }

    public void addOneStem(String paperId, String stemId) {
        PaperTopic paperTopic = new PaperTopic();
        paperTopic.setPaperId(paperId).setTopicId(stemId);
            paperTopicMapper.insert(paperTopic);

    }
}
